package com.tjnu.losca.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tjnu.losca.core.ResponseVO;
import com.tjnu.losca.form.EmployeeFrom;
import com.tjnu.losca.pojo.Employee;
import com.tjnu.losca.service.IEmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author losca
 * @since 2021-09-13
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/getAll")
    public ResponseVO<IPage> getAll(@RequestParam(defaultValue = "1") int pageNum,
                                            @RequestParam(defaultValue = "3") int pageSize) {
        Page<Employee> page = new Page<>(pageNum, pageSize);
        return ResponseVO.success("成功",employeeService.page(page));
    }

    @GetMapping("/getEmployeesByname")
    public ResponseVO<IPage> getEmployeesByname(@RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "3") int pageSize,
                                              @RequestParam(defaultValue = "") String name) {
        return ResponseVO.success("成功",employeeService.getEmployee(pageNum, pageSize, name));
    }

    @PostMapping("/save")
    public ResponseVO<Boolean> save(@RequestBody EmployeeFrom form) {
        Employee employee = new Employee();
        //将form对象转化为Employee对象
        BeanUtils.copyProperties(form, employee);
        return ResponseVO.success("保存成功",employeeService.saveOrUpdate(employee));
    }

    @GetMapping("/getEmployeeById")
    public ResponseVO<Employee> getEmployeeById(Integer id) {
        return ResponseVO.success("成功",employeeService.getById(id));
    }

    @GetMapping("/del")
    public ResponseVO<Boolean> del(Integer id){
        return ResponseVO.success("成功",employeeService.removeById(id));
    }




}
